﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace HandRise.MBHOP.Controls
{
    public class PlatformAPI
    {
        const int SHFS_SHOWTASKBAR = 0x0001;
        const int SHFS_HIDETASKBAR = 0x0002;
        const int SHFS_SHOWSIPBUTTON = 0x0004;
        const int SHFS_HIDESIPBUTTON = 0x0008;
        const int SHFS_SHOWSTARTICON = 0x0010;
        const int SHFS_HIDESTARTICON = 0x0020;
        const int WS_TABSTOP = 0x00010000;

        const int GWL_STYLE = -16;
        const int GWL_EXSTYLE = -20;

        const int SHIDIM_FLAGS = 0x0001;

        const int SHIDIF_DONEBUTTON = 0x0001;
        const int SHIDIF_SIZEDLG = 0x0002;
        const int SHIDIF_SIZEDLGFULLSCREEN = 0x0004;
        const int SHIDIF_SIPDOWN = 0x0008;
        const int SHIDIF_FULLSCREENNOMENUBAR = 0x0010;
        const int SHIDIF_EMPTYMENU = 0x0020;
        const int SHIDIF_WANTSCROLLBAR = 0x0040;

        public const uint WS_VISIBLE = 0x10000000;
        public const uint WS_EX_TOOLWINDOW = 0x00000080;

        public struct SHINITDLGINFO
        {
            public int dwMask;
            public IntPtr hDlg;
            public int dwFlags;
        } ;


        [DllImport("aygshell.dll", CharSet = CharSet.Auto)]
        public static extern int SHInitDialog(ref SHINITDLGINFO pshidi);


        [DllImport("aygshell.dll", CharSet = CharSet.Auto)]
        public static extern int SHFullScreen(IntPtr hwndRequester, int dwState);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        public static extern int SetForegroundWindow(IntPtr hWnd);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, int bRepaint);

        [DllImport("coredll.dll")]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("coredll.dll")]
        public static extern uint GetWindowLong(IntPtr hWnd, int nIndex);


        const int SM_CXSCREEN = 0;
        const int SM_CYSCREEN = 1;
        [DllImport("coredll.dll")]
        public static extern int GetSystemMetrics(int nIndex);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow([MarshalAs(UnmanagedType.LPWStr)]string clsName,
            [MarshalAs(UnmanagedType.LPWStr)]string caption);


        public const int SW_HIDE = 0;
        public const int SW_SHOW = 5;
        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public static void makeItFullScr(Form form)
        {
            foreach (Control ctrl in form.Controls)
            {
                ctrl.TabStop = false;
            }

            form.Menu = null;

            SetForegroundWindow(form.Handle);
            SHFullScreen(form.Handle, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
            hideTaskBar();
        }

        public static void toRegularScr(Form form)
        {
            SHFullScreen(form.Handle, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
            showTaskBar();
        }

        public static void hideTaskBar()
        {
            IntPtr hwnd = FindWindow("HHTaskBar", null);
            ShowWindow(hwnd, SW_HIDE);
        }
        public static void showTaskBar()
        {
            IntPtr hwnd = FindWindow("HHTaskBar", null);
            ShowWindow(hwnd, SW_SHOW);
        }

        internal struct BlendFunction
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }

        internal enum BlendOperation : byte
        {
            AC_SRC_OVER = 0x00
        }

        internal enum BlendFlags : byte
        {
            Zero = 0x00
        }

        internal enum SourceConstantAlpha : byte
        {
            Transparent = 0x00,
            Opaque = 0xFF
        }

        internal enum AlphaFormat : byte
        {
            AC_SRC_ALPHA = 0x01
        }

        internal class DrawingAPI
        {
            [DllImport("coredll.dll")]
            extern internal static bool AlphaBlend(IntPtr hdcDest, Int32 nXDest, Int32 nYDest, Int32 nWidthDst, Int32 nHeightDst, IntPtr hdcSrc, Int32 nXSrc, Int32 nYSrc, Int32 nWidthSrc, Int32 nHeightSrc, BlendFunction blendFunction);

            [DllImport("coredll.dll")]
            extern internal static bool BitBlt(IntPtr hdcDest, Int32 nXDest, Int32 nYDest, Int32 nWidth, Int32 nHeight, IntPtr hdcSrc, Int32 nXSrc, Int32 nYSrc, UInt32 dwRop);

            internal const UInt32 SRCCOPY = 0x00CC0020;
        }

        internal static void DrawAlpha(IntPtr hdcDst, Bitmap image, byte transp, int x, int y)
        {
            using (Graphics gxSrc = Graphics.FromImage(image))
            {
                //IntPtr hdcDst = gx.GetHdc();
                IntPtr hdcSrc = gxSrc.GetHdc();
                BlendFunction blendFunction = new BlendFunction();
                blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                blendFunction.SourceConstantAlpha = transp;                 // Constant alpha factor
                blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha
                DrawingAPI.AlphaBlend(hdcDst, x, y, image.Width, image.Height, hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
                //gx.ReleaseHdc(hdcDst);          // Required cleanup to GetHdc()
                gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
            }
        }
    }
}
